package com.l024.android_ljl.view.main.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import androidx.recyclerview.widget.LinearLayoutManager;

import com.hjq.toast.ToastUtils;
import com.l024.android_ljl.MainActivity;
import com.l024.android_ljl.R;
import com.l024.android_ljl.constant.PageConstant;
import com.l024.android_ljl.databinding.ActivitySubjectBinding;
import com.l024.android_ljl.entity.MessageWrap;
import com.l024.android_ljl.entity.SysCommentEntity;
import com.l024.android_ljl.entity.SysOptionEntity;
import com.l024.android_ljl.entity.SysSubjectEntity;
import com.l024.android_ljl.viewmodel.main.SubjectViewModel;
import com.quantstack.llcommonlibrary.adapter.CommonAdapter;
import com.quantstack.llcommonlibrary.adapter.CommonViewHolder;
import com.quantstack.llcommonlibrary.base.BaseActivity;
import com.quantstack.llcommonlibrary.base.BaseViewModel;
import com.quantstack.llcommonlibrary.event.OnClickEvent;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 试卷页面
 */
public class SubjectActivity extends BaseActivity<SubjectViewModel, ActivitySubjectBinding> implements BaseViewModel.OnPageDataListener {

    public final static int UPDATE_OPTIONS = 1000;
    @Override
    public int getLayoutId() {
        return R.layout.activity_subject;
    }

    @Override
    public void afterCreate() {
        mViewModel.setBaseView(this);
        mViewModel.setOnPageDataListener(this);
        mViewDataBind.setModel(mViewModel);
        EventBus.getDefault().register(this);
        initComment();
        mViewDataBind.btBack.setOnClickListener(new OnClickEvent() {
            @Override
            public void singleClick(View v) {
                go(SubjectActivity.this, MainActivity.class);
            }
        });

        Intent intent = getIntent();
        if(intent!=null){
            Bundle bundle = intent.getBundleExtra("bundle");
            if(bundle!=null){
                long typeId = bundle.getLong("typeId");
                mViewModel.getSubjectsByTypeId(typeId);
                return;

            }
        }
        mViewModel.getSubjects();

    }
    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onGetMessage(MessageWrap message) {
       if(message!=null){
           switch (message.getCode()){
               case UPDATE_OPTIONS:{
                   //更新选项
                   addOptions();
                   break;
               }
           }
       }
    }


    @Override
    public void onSuccess(String tag) {
        switch (tag){
            case PageConstant.SUBJECTS:{
                ToastUtils.show("获取成功");
                addOptions();
                break;
            }
            case PageConstant.SUBJECTS_COMMENTS:{
                list.clear();
                List<SysCommentEntity> value = mViewModel.getComments().getValue();
                list.addAll(value);
                adapter.notifyDataSetChanged();
                break;
            }
        }
    }

    @Override
    public void onError(String tag, String msg) {
        switch (tag){
            case PageConstant.SUBJECTS:{
                ToastUtils.show(msg);
                break;
            }
            case PageConstant.SUBJECTS_COMMENTS:{

                break;
            }
        }
    }

    //更新选项
    private void addOptions(){
        //渲染数据
        SysSubjectEntity subject = mViewModel.getCurrentSubject().getValue();
        if(subject!=null){
            List<SysOptionEntity> options = subject.getOptions();
            if(options!=null&&options.size()>0){
                mViewDataBind.llOptions.removeAllViews();
                for(SysOptionEntity optionEntity:options){
                    TextView view = new TextView(this);
                    view.setText(optionEntity.getContent());
                    view.setTextSize(18);
                    view.setTextColor(getResources().getColor(R.color.c333));
                    mViewDataBind.llOptions.addView(view);
                }
            }
        }
    }


    /**
     * 初始化评论
     */
    private SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    List<SysCommentEntity> list = new ArrayList<>();
    CommonAdapter<SysCommentEntity> adapter;
    private void initComment(){
            adapter = new CommonAdapter<SysCommentEntity>(list, new CommonAdapter.OnBindDataListener<SysCommentEntity>() {
            @Override
            public void onBindViewHolder(SysCommentEntity model, CommonViewHolder viewHolder, int type, int position) {
                viewHolder.setImageUrl(SubjectActivity.this,R.id.iv_avator,model.getUser().getAvator());
                viewHolder.setText(R.id.tv_username,model.getUser().getName());
                viewHolder.setText(R.id.tv_comment,model.getComment());
                String sd = sdf.format(new Date(model.getCommentTime())); // 时间戳转换日期
                viewHolder.setText(R.id.tv_time,sd);
            }

            @Override
            public int getLayoutId(int type) {
                return R.layout.item_comment;
            }
        });
        mViewDataBind.rvComment.setLayoutManager(new LinearLayoutManager(this));
        mViewDataBind.rvComment.setAdapter(adapter);
    }
}